<template>
    <div class="post-form">
      <h2>发布帖子</h2>
      
      <!-- 帖子内容 -->
      <textarea v-model="postContent" placeholder="请输入帖子内容" rows="4"></textarea>
  
      <!-- 按钮容器 -->
      <div class="button-container">
        <!-- 插入表情 -->
        <button @click="toggleEmojiPicker" class="emoji-button">选择表情</button>
        <!-- 提交按钮 -->
        <button @click="submitPost" :disabled="!canSubmit">发布帖子</button>
      </div>
  
      <div v-if="showEmojiPicker" class="emoji-list">
        <span v-for="emoji in emojis" :key="emoji" @click="insertEmoji(emoji)" class="emoji">
          {{ emoji }}
        </span>
      </div>
  
      <!-- 图片上传 -->
      <div class="image-upload">
        <input type="file" @change="handleImageUpload" multiple accept="image/*" />
        <p v-if="images.length >= 9">最多上传9张图片</p>
        <div class="image-previews">
          <div v-for="(image, index) in images" :key="index" class="image-preview-container">
            <img :src="image" alt="Uploaded image" class="image-preview" />
            <span class="emoji-delete" @click="removeImage(index)">❌</span>
          </div>
        </div>
      </div>
    </div>
  </template>
  
  <script>
  export default {
    data() {
      return {
        postContent: '', 
        images: [], 
        showEmojiPicker: false, 
        emojis: ['😀', '😁', '😂', '🤣', '😃', '😄', '😅', '😆', '😇', '😈'],
      };
    },
    computed: {
      canSubmit() {
        return this.postContent.trim() !== '' || this.images.length > 0;
      },
    },
    methods: {
      toggleEmojiPicker() {
        this.showEmojiPicker = !this.showEmojiPicker;
      },
      insertEmoji(emoji) {
        this.postContent += emoji;
        this.showEmojiPicker = false;
      },
      handleImageUpload(event) {
        const files = Array.from(event.target.files);
        if (this.images.length + files.length > 9) {
          alert('最多上传9张图片');
          return;
        }
        files.forEach(file => {
          const reader = new FileReader();
          reader.onload = () => {
            this.images.push(reader.result);
          };
          reader.readAsDataURL(file);
        });
      },
      removeImage(index) {
        this.images.splice(index, 1); 
      },
      submitPost() {
        alert('帖子已发布');
        this.postContent = '';
        this.images = [];
      },
    },
  };
  </script>
  
  <style scoped>
  .post-form {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    border: 2px solid #ccc;
    padding: 20px;
    width: 800px;
    margin: 0 auto;
    background-color: #f9f9f9;
    border-radius: 10px;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
    overflow-y: auto;
  }
  
  h2 {
    text-align: center;
    font-size: 1.8em;
    color: #333;
    margin-bottom: 30px;
    font-weight: bold; 
  }
  
  textarea {
    width: 700px;
    padding: 8px;
    margin-bottom: 8px; 
    border: 1px solid #ddd;
    border-radius: 8px;
    font-size: 16px;
  }
  
  .emoji-list {
    display: flex;
    flex-wrap: wrap;
    gap: 8px; 
  }
  
  .emoji {
    font-size: 20px;
    cursor: pointer;
  }
  
  .emoji-button {
    padding: 8px 12px; 
    font-size: 14px;
  }
  
  .button-container {
    display: flex;
    justify-content: center;
    gap: 10px;
    margin-bottom: 8px; 
  }
  
  .button-container button {
    width: 250px; 
    padding: 8px;
    font-size: 14px;
  }
  
  .image-upload {
    margin-top: 10px;
  }
  
  input[type="file"] {
    padding: 8px;
  }
  
  .image-previews {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
  }
  
  .image-preview-container {
    position: relative;
  }
  
  .image-preview {
    width: 90px;
    height: 90px;
    object-fit: cover;
    border-radius: 8px;
    border: 1px solid #ddd;
  }
  
  .emoji-delete {
    position: absolute;
    top: 5px;
    right: 5px;
    font-size: 16px;
    cursor: pointer;
    color: red;
  }
  
  button {
    padding: 8px 16px;
    background-color: #0084ff;
    color: white;
    border: none;
    border-radius: 8px;
    cursor: pointer;
  }
  
  button:disabled {
    background-color: #ccc;
    cursor: not-allowed;
  }
  </style>